# FM Best Framework Makefile

.PHONY: help build run test clean fmt clippy check dev docker-build docker-run docker-stop migrate setup install

# Default target
help: ## Show this help message
	@echo "FM Best Framework - Available commands:"
	@echo ""
	@grep -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

# Development commands
setup: ## Setup development environment
	@echo "Setting up development environment..."
	cp .env.example .env
	@echo "Please edit .env file with your configuration"
	@echo "Setup complete!"

install: ## Install dependencies
	@echo "Installing dependencies..."
	cargo fetch

dev: ## Run in development mode with auto-reload
	@echo "Starting development server..."
	cargo watch -x run

run: ## Run the application
	@echo "Starting application..."
	cargo run

build: ## Build the application
	@echo "Building application..."
	cargo build

build-release: ## Build the application in release mode
	@echo "Building application in release mode..."
	cargo build --release

# Testing and quality
test: ## Run tests
	@echo "Running tests..."
	cargo test

test-coverage: ## Run tests with coverage
	@echo "Running tests with coverage..."
	cargo tarpaulin --out Html --output-dir coverage

fmt: ## Format code
	@echo "Formatting code..."
	cargo fmt

clippy: ## Run clippy linter
	@echo "Running clippy..."
	cargo clippy -- -D warnings

check: fmt clippy test ## Run all checks (format, lint, test)
	@echo "All checks completed!"

# Database commands
migrate: ## Run database migrations
	@echo "Running database migrations..."
	cargo run --bin migration

migrate-fresh: ## Fresh migration (reset and migrate)
	@echo "Running fresh migration..."
	cargo run --bin migration -- fresh

migrate-reset: ## Reset database
	@echo "Resetting database..."
	cargo run --bin migration -- reset

generate-entity: ## Generate SeaORM entities from database
	@echo "Generating entities..."
	sea-orm-cli generate entity -o entity/src

# Docker commands
docker-build: ## Build Docker image
	@echo "Building Docker image..."
	docker build -t fm-best .

docker-run: ## Run application in Docker
	@echo "Running application in Docker..."
	docker-compose up -d

docker-stop: ## Stop Docker containers
	@echo "Stopping Docker containers..."
	docker-compose down

docker-logs: ## Show Docker logs
	@echo "Showing Docker logs..."
	docker-compose logs -f app

docker-clean: ## Clean Docker resources
	@echo "Cleaning Docker resources..."
	docker-compose down -v
	docker system prune -f

# Production commands
deploy-staging: ## Deploy to staging environment
	@echo "Deploying to staging..."
	docker-compose -f docker-compose.yml -f docker-compose.staging.yml up -d

deploy-prod: ## Deploy to production environment
	@echo "Deploying to production..."
	docker-compose -f docker-compose.yml -f docker-compose.prod.yml up -d

# Monitoring commands
monitoring-up: ## Start monitoring stack
	@echo "Starting monitoring stack..."
	docker-compose --profile monitoring up -d

monitoring-down: ## Stop monitoring stack
	@echo "Stopping monitoring stack..."
	docker-compose --profile monitoring down

# Utility commands
clean: ## Clean build artifacts
	@echo "Cleaning build artifacts..."
	cargo clean
	rm -rf target/
	rm -rf logs/*.log

backup-db: ## Backup database
	@echo "Backing up database..."
	@mkdir -p backups
	docker-compose exec postgres pg_dump -U fm_user fm_best_db > backups/backup_$(shell date +%Y%m%d_%H%M%S).sql

restore-db: ## Restore database from backup (usage: make restore-db BACKUP=backup_file.sql)
	@echo "Restoring database from $(BACKUP)..."
	docker-compose exec -T postgres psql -U fm_user -d fm_best_db < backups/$(BACKUP)

logs: ## Show application logs
	@echo "Showing application logs..."
	tail -f logs/app.log

health: ## Check application health
	@echo "Checking application health..."
	curl -f http://localhost:8080/health || echo "Application is not running"

# Security commands
security-audit: ## Run security audit
	@echo "Running security audit..."
	cargo audit

update-deps: ## Update dependencies
	@echo "Updating dependencies..."
	cargo update

# Documentation
docs: ## Generate documentation
	@echo "Generating documentation..."
	cargo doc --open

# Benchmarks
bench: ## Run benchmarks
	@echo "Running benchmarks..."
	cargo bench

# Quick development workflow
quick: check run ## Quick development workflow (check + run)

# Full CI workflow
ci: clean check test build ## Full CI workflow
	@echo "CI workflow completed successfully!"

# Environment specific targets
dev-env: ## Setup development environment
	@echo "Setting up development environment..."
	cp .env.example .env
	sed -i 's/RUST_ENV=production/RUST_ENV=development/' .env
	sed -i 's/LOG_LEVEL=warn/LOG_LEVEL=debug/' .env

test-env: ## Setup test environment
	@echo "Setting up test environment..."
	cp .env.example .env.test
	sed -i 's/fm_best_db/fm_best_test_db/' .env.test
	sed -i 's/RUST_ENV=production/RUST_ENV=test/' .env.test

prod-env: ## Setup production environment
	@echo "Setting up production environment..."
	cp .env.example .env.prod
	@echo "Please configure production settings in .env.prod"

# Performance profiling
profile: ## Run performance profiling
	@echo "Running performance profiling..."
	cargo build --release
	perf record --call-graph=dwarf target/release/fm_best
	perf script | inferno-collapse-perf | inferno-flamegraph > flamegraph.svg

# Load testing
load-test: ## Run load tests
	@echo "Running load tests..."
	@command -v wrk >/dev/null 2>&1 || { echo "wrk is required for load testing. Install it first."; exit 1; }
	wrk -t12 -c400 -d30s http://localhost:8080/health

# Database utilities
db-shell: ## Connect to database shell
	@echo "Connecting to database..."
	docker-compose exec postgres psql -U fm_user -d fm_best_db

db-reset: migrate-reset migrate ## Reset and migrate database
	@echo "Database reset completed!"

# Version management
version: ## Show current version
	@echo "Current version:"
	@grep '^version' Cargo.toml | head -1

bump-patch: ## Bump patch version
	@echo "Bumping patch version..."
	cargo bump patch

bump-minor: ## Bump minor version
	@echo "Bumping minor version..."
	cargo bump minor

bump-major: ## Bump major version
	@echo "Bumping major version..."
	cargo bump major